package com.xiaoyu.studyworkspace.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.xiaoyu.studyworkspace.common.kapaentity.websocket.KapaCode;
import com.xiaoyu.studyworkspace.common.kapaentity.selectrecord.JsonRootBean;
import com.xiaoyu.studyworkspace.common.util.KapaHttpUtil;
import com.xiaoyu.studyworkspace.common.util.KapaParam;
import com.xiaoyu.studyworkspace.common.util.RoomName;
import com.xiaoyu.studyworkspace.dto.RecordDTO;
import com.xiaoyu.studyworkspace.dto.RecordSizeDTO;
import com.xiaoyu.studyworkspace.mapper.RecordMapper;
import com.xiaoyu.studyworkspace.pojo.Notice;
import com.xiaoyu.studyworkspace.pojo.RecordV3;
import com.xiaoyu.studyworkspace.properties.UserCheckKapaProperties;
import com.xiaoyu.studyworkspace.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Autowired
    private UserCheckKapaProperties userCheckKapaProperties;

    @Override
    public List<RecordV3> getRecordsV3(RecordSizeDTO recordSizeDTO) {
        //从当中缓存中查
        List<RecordV3> recordV3s = (List<RecordV3>) redisTemplate.opsForValue().get("recordV3s");
        //缓存未命中再时候从数据库当中查
        if (recordV3s == null) {
            List<RecordV3> recordV2sRead = recordMapper.slectRecordsV3BySzie(recordSizeDTO.getSize());
            //读取放入缓存当中
            redisTemplate.opsForValue().set("recordV3s", recordV2sRead);
            return recordV2sRead;
        }
        return recordV3s;
    }


    @Override
    public List<Notice> getNotice() {
        List<Notice> noticeList = recordMapper.selectNotice();
        return noticeList;
    }


    /**
     * v3版本
     *
     * @param recordDTO
     */
    @Override
    public void insertRecordV3(RecordDTO recordDTO) {
        RecordV3 recordV3 = new RecordV3();
        //取出上条记录
        RecordV3 recordLastV3 = recordMapper.selectRecordLastV3();
        if (recordLastV3 == null) {
            RecordV3 recordV31 = new RecordV3();
            recordV31.setRoom_name("初始化");
            recordV31.setBishui_win(0);
            recordV31.setHanyan_win(0);
            recordV31.setQingtian_win(0);
            recordV31.setYueliang_win(0);
            recordV31.setTingyu_win(0);
            recordV31.setCreate_time(LocalDateTime.now());
            recordMapper.insertRecordV3(recordV31);
            recordLastV3 = recordV31;
            //插入一条虚拟记录
        }
        String killName = recordDTO.getRoom_name();
        recordV3.setRoom_name(killName);
        recordV3.setBishui_win(RoomName.bishui.equals(killName) ? 0 : recordLastV3.getBishui_win() + 1);
        recordV3.setHanyan_win(RoomName.hanyan.equals(killName) ? 0 : recordLastV3.getHanyan_win() + 1);
        recordV3.setQingtian_win(RoomName.qingtian.equals(killName) ? 0 : recordLastV3.getQingtian_win() + 1);
        recordV3.setYueliang_win(RoomName.yueliang.equals(killName) ? 0 : recordLastV3.getYueliang_win() + 1);
        recordV3.setTingyu_win(RoomName.tingyu.equals(killName) ? 0 : recordLastV3.getTingyu_win() + 1);
        recordV3.setCreate_time(LocalDateTime.now());
        //取出最后一把的id
        //调用接口，返回用户的数据
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put(KapaParam.CODE, KapaCode.SELECT_RECORD);
        paramMap.put(KapaParam.DATA, "%7B%7D");
        paramMap.put(KapaParam.DEPLOY_APP_ID, userCheckKapaProperties.getDeploy_app_id());
        paramMap.put(KapaParam.INIT_ID, userCheckKapaProperties.getInit_id());
        paramMap.put(KapaParam.SESSION_ID, userCheckKapaProperties.getSession_id());
        String kapaResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
        JsonRootBean jsonRootBean = JSONUtil.toBean(kapaResponse, JsonRootBean.class);
        recordV3.setGames(jsonRootBean.getData().getRecords().get(0).getGame_no() + 1);
        //插入
        recordMapper.insertRecordV3(recordV3);
        //读取放入缓存当中
        List<RecordV3> recordV3s = recordMapper.slectRecordsV3BySzie(100);
        redisTemplate.opsForValue().set("games", recordV3.getGames());
        redisTemplate.opsForValue().set("recordV3s", recordV3s);
        redisTemplate.expire("recordV3s", 1, TimeUnit.HOURS);
    }


}
